import Vue from 'vue'
import Vuex from 'vuex'
import login from './modules/login'

Vue.use(Vuex)
const saveCongig = [
  {
    mutationName: 'LOGIN_',
    stateName: 'login',
    sessionName: '$_login'
  },
  {
    mutationName: 'DISCUSS_',
    stateName: 'roastTalking',
    sessionName: '$_roastTalking'
  },
  {
    mutationName: 'GIFT_',
    stateName: 'gift',
    sessionName: '$_gift'
  }
]

const saveToSessionStorage = (store) => {
  store.subscribe((mutation, state) => {
    for (let index = 0; index < saveCongig.length; index++) {
      const item = saveCongig[index]
      if (mutation.type.startsWith(item.mutationName)) {
        sessionStorage.setItem(item.sessionName, JSON.stringify(state[item.stateName]))
      }
    }
  })
}

export default new Vuex.Store({
  state: {},
  mutations: {},
  actions: {},
  modules: { login },
  plugins: [saveToSessionStorage]
})

/* import { createStore } from 'vuex'
import { mutations } from './mutations'
import native from "../../../utils/native";
import activityPlan from './modules/activityPlan'
import customerDetail from './modules/customer-detail'
import business from './modules/business'
import monthlyPlan from './modules/monthlyPlan'
import insurancePolicy from './modules/insurancePolicy'
import enquiryManage from './modules/enquiryManage'
import premiumTrial from './modules/premiumTrial'
import clauseSearch from './modules/clauseSearch'
import caseShare from './modules/caseShare'
import customerFeature from './modules/customerFeature'
const loginToken = sessionStorage.getItem('$_loginToken')
const user = native.getUserInfoSync() || { userName: undefined, umNo: undefined, loginToken: loginToken }

const cacheList = [
  { startName: 'ACTIVITYPLAN_', storeName: 'activityPlan' }, // 活动计划
  { startName: 'CUSTOMERDETAIL_', storeName: 'customerDetail' },
  { startName: 'BUSINESS_', storeName: 'business' },
  { startName: 'MONTHLYPLAN_', storeName: 'monthlyPlan' },
  { startName: 'INSURANCEPOLICY_', storeName: 'insurancePolicy' },
  { startName: 'ENQUIRYMANAGE_', storeName: 'enquiryManage' },
  { startName: 'PREMIUMTRIAL_', storeName: 'premiumTrial' },
  { startName: 'CLAUSESEARCH_', storeName: 'clauseSearch' },
  { startName: 'CASESHARE_', storeName: 'caseShare' },
  { startName: 'CUSTOMERFEATURE_', storeName: 'customerFeature' }
]

const cacheFn = (startWith, storeName, mutation, state) => {
  if (mutation.type.startsWith(startWith)) {
    try {
      console.log('state[storeName]', state[storeName])
      sessionStorage.setItem(`$_${storeName}_state`, JSON.stringify(state[storeName]))
    } catch (e) {
      console.log('sessionStorage write error', e)
    }
  }
}
// store同步到本地插件
const storeToLocal = (store) => {
  store.subscribe((mutation, state) => {
    cacheList.forEach(item => {
      cacheFn(item.startName, item.storeName, mutation, state)
    })
  })
}

export default createStore({
  state: {
    user: user,
    isIphonePing: false, // IOS 有刘海屏的手机
    sysId: 'EGIS_ISP', // 系统ID
    pageId: sessionStorage.getItem('$_pageID') || '1000-1000-1000', // 页面编号（写死在配置文件里，默认值是为了给srcPageId赋初始值）
    idTrackPage: sessionStorage.getItem('$_idTrackPage') || '', // 访问记录ID（动态生成，每次进来都不一样）
    srcPageId: sessionStorage.getItem('$_srcPageID') || '', // 源页面ID（上一个页面的页面ID）
    srcTrackId: sessionStorage.getItem('$_srcTrackId') || '', // 源访问记录ID（上一个页面的访问记录ID）
    pageUrl: '', // 页面全路径
    nativePdfInfo: {
      base64Pdf: '',
      pdfUrl: '',
      pageTitle: '' // 页面标题
    }, // native 使用PDF信息 base64Pdf与pdfUrl只允许一个,区分base64文件还是链接地址
  },
  mutations,
  actions: {
  },
  modules: {
    activityPlan,
    customerDetail,
    business,
    monthlyPlan,
    insurancePolicy,
    enquiryManage,
    premiumTrial,
    clauseSearch,
    caseShare,
    customerFeature
  },
  plugins: [storeToLocal]
}) */